package com.softeem.score.manager.controller.admin;

import com.github.pagehelper.PageInfo;
import com.softeem.score.common.log.annotation.Log;
import com.softeem.score.common.log.enums.OperatorType;
import com.softeem.score.manager.service.RecordScoreService;
import com.softeem.score.model.dto.gradeDto.RecordScoreDto;
import com.softeem.score.model.entity.educational.GradeDetail;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.educational.GradeVo;
import com.softeem.score.model.vo.school.MapVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

@Tag(name = "录入成绩接口")
@RestController
@RequestMapping(value = "/admin/educational/recordScore")
public class RecordScoreController {

    @Autowired
    private RecordScoreService recordScoreService;

    @Log(title = "通过课程id和教师id查询班级信息", businessType = 0, operatorType = OperatorType.TEACHER)
    @Operation(summary = "通过课程id和教师id查询班级信息")
    @GetMapping(value = "/findAllTeachingClassByCourseIdAndTeacherId/{courseId}/{teacherId}")
    public Result findAllTeachingClassByCourseIdAndTeacherId(@PathVariable(value = "courseId") Long courseId,
                                                             @PathVariable(value = "teacherId") Long teacherId,
                                                             @RequestParam(value = "academicYear", required = false) String academicYear,
                                                             @RequestParam(value = "semester", required = false) Integer semester) {
        List<MapVo> allClasses = recordScoreService.findAllTeachingClassByCourseIdAndTeacherId(courseId, teacherId, academicYear, semester);
        return Result.build(allClasses, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "获取学生成绩", businessType = 0, operatorType = OperatorType.TEACHER)
    @Operation(summary = "获取学生成绩")
    @GetMapping(value = "findGradeInfoByPage/{pageNum}/{pageSize}/{teachingClassId}")
    public Result<PageInfo<GradeVo>> findGradeInfoByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                         @PathVariable(value = "pageSize") Integer pageSize,
                                                         @PathVariable(value = "teachingClassId") Long teachingClassId) {
        PageInfo<GradeVo> pageInfo = recordScoreService.findGradeInfoByPage(pageNum, pageSize, teachingClassId);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }


    @Log(title = "保存成绩", businessType = 0, operatorType = OperatorType.TEACHER)
    @Operation(summary = "保存成绩")
    @PostMapping(value = "/saveGradeDetailInfo")
    public Result saveGradeDetailInfo(@RequestBody List<GradeDetail> gradeDetailList) {
        System.out.println("gradeDetailList = " + gradeDetailList);
        recordScoreService.saveGradeDetailInfo(gradeDetailList);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新总成绩", businessType = 0, operatorType = OperatorType.TEACHER)
    @Operation(summary = "更新总成绩")
    @PutMapping(value = "/updateTotalScore/{id}/{totalScore}")
    public Result updateTotalScore(@PathVariable(value = "id") Long id,
                                   @PathVariable(value = "totalScore") BigDecimal totalScore) {
        recordScoreService.updateTotalScore(id, totalScore);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "导出成绩", businessType = 0, operatorType = OperatorType.TEACHER)
    @Operation(summary = "导出成绩")
    @PostMapping("downloadTemplate")
    public ResponseEntity<byte[]> downloadTemplate(@RequestBody RecordScoreDto recordScoreDto) {
        return recordScoreService.exportData(recordScoreDto);
    }

    @Log(title = "导入数据", businessType = 1, operatorType = OperatorType.TEACHER)
    @Operation(summary = "导入数据")
    @PostMapping("importData")
    public Result importData(@RequestParam("file") MultipartFile file, @RequestParam("teachingClassId") Long teachingClassId) {
        recordScoreService.importData(file, teachingClassId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
